package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Department;
import com.example.entity.Doctor;
import com.example.entity.Registration;
import com.example.entity.User;
import com.example.entity.vo.RegistrationVo;
import com.example.service.IDepartmentService;
import com.example.service.IDoctorService;
import com.example.service.IRegistrationService;
import com.example.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 挂号帖 前端控制器
 * </p>
 *
 * @author wyx
 * @since 2025-03-17
 */
@CrossOrigin//跨域访问
@Slf4j
@RestController
@Api(tags = "挂号帖管理模块")
@RequestMapping("/registration")
public class RegistrationController {
    @Autowired
    IRegistrationService registrationService;
    @Autowired
    IDoctorService doctorService;
    @Autowired
    IUserService userService;
    @Autowired
    IDepartmentService departmentService;

    //分页查询挂号帖列表
    @ApiOperation(value = "分页查询挂号帖列表")
    @GetMapping("/page")
    public R page(
            @RequestParam(required = false) String description,
            @RequestParam(required = false) Integer diseaseType,
            @RequestParam(required = false) Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {

        log.info("根据条件分页查询挂号记录, 疾病类型: {}, 用户ID: {}, 描述: {}, 页码: {}, 每页大小: {}", diseaseType, userId, description, page, pageSize);

        // 创建分页对象
        Page<Registration> pageInfo = new Page<>(page, pageSize);
        Page<RegistrationVo> pageInfo1 = new Page<>(page, pageSize);

        // 创建查询条件
        LambdaQueryWrapper<Registration> queryWrapper = new LambdaQueryWrapper<>();

        // 如果提供了疾病类型参数，则添加疾病类型查询条件
        if (diseaseType != null) {
            queryWrapper.eq(Registration::getDeptId, diseaseType);
        }

        // 如果提供了用户ID参数，则添加用户ID查询条件
        if (userId != null) {
            queryWrapper.eq(Registration::getUserId, userId);
        }

        // 如果提供了描述参数，则添加描述查询条件
        if (description != null && !description.isEmpty()) {
            queryWrapper.like(Registration::getDescription, description);
        }

        // 执行分页查询
        registrationService.page(pageInfo, queryWrapper);

        // 将分页查询结果转换为 RegistrationVo
        List<RegistrationVo> registrationVoList = pageInfo.getRecords().stream()
                .map(registration -> {
                    RegistrationVo vo = new RegistrationVo();
                    BeanUtils.copyProperties(registration, vo);
                    vo.setStatusName(registration.getStatus() == 0 ? "待接诊" : registration.getStatus() == 1 ? "已接诊" : "已完成");
                    // 处理部门名称
                    Department department = departmentService.getById(registration.getDeptId());
                    vo.setDeptName(department != null ? department.getName() : "未知部门");

                    // 处理医生名称
                    Doctor doctor = doctorService.getById(registration.getDocId());
                    vo.setDoctName(doctor != null ? doctor.getName() : "未知医生");

                    // 处理用户名称
                    User user = userService.getById(registration.getUserId());
                    vo.setUserName(user != null ? user.getName() : "未知用户");
                    return vo;
                })
                .collect(Collectors.toList());

        // 设置分页信息和转换后的 records
        BeanUtils.copyProperties(pageInfo, pageInfo1, "records");
        pageInfo1.setRecords(registrationVoList);

        // 返回结果
        return R.success(pageInfo1);
    }

    //添加挂号帖
    @ApiOperation(value = "新增挂号帖")
    @PostMapping("/add")
    public R addRegistration(Registration registration) {
        log.info("新增挂号帖: {}", registration);
        registrationService.save(registration);
        return R.success("新增挂号帖成功");
    }

    //删除挂号帖
    @ApiOperation(value = "删除挂号帖")
    @DeleteMapping("/delete/{id}")
    public R deleteRegistration(@PathVariable Integer id) {
        log.info("删除挂号帖, ID: {}", id);
        registrationService.removeById(id);
        return R.success("删除挂号帖成功");
    }

    @ApiOperation(value = "修改挂号帖")
    @PutMapping("/update")
    public R updateRegistration(Registration registration) {
        log.info("修改挂号帖: {}", registration);
        registrationService.updateById(registration);
        return R.success("修改挂号帖成功");
    }
}
